package LeetCode.month02.day45;

import java.util.ArrayDeque;
import java.util.Deque;

public class No20 {
    public boolean isValid(String s) {
        Deque<Character> queue = new ArrayDeque<>();
        int n = s.length();
        for (int i = 0; i < n; i++) {
            char c = s.charAt(i);
            if(queue.isEmpty()&&(c==')'||c=='}'||c==']')) {
                return false;
            }
            if(c==')'){
                if(queue.poll()!='(') return false;
            }else if(c=='}'){
                if (queue.poll()!='{') return false;
            }else if(c==']'){
                if(queue.poll()!='[') return false;
            }else{
                queue.push(c);
            }
        }
        if(!queue.isEmpty()) return false;
        return true;
    }
}
